package MediaOnline.WebService;

import java.io.IOException;
import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import MediaOnline.Adapter.VideoAdapter;
import MediaOnline.Data.FilmCatData;
import MediaOnline.GUI.actFilmMain3;
import MediaOnline.General.paramConst;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class getFilmCat extends AsyncTask<String, Void, String> {
	//Khai bao thong tin can thiet de goi web service
	public Context mcontext=null;
	private static final String Soap_MethodName = "AndroidGetFilmCategoriesByWhereFillNull";
	private static final String Soap_Action = paramConst.Webservice_NameSpace+Soap_MethodName;
	private static final String Soap_NameSpace = paramConst.Webservice_NameSpace;
	private static final String Soap_URL = paramConst.Webservice_Url+"FilmCategoriesService.asmx?op="+Soap_MethodName;
	final SoapObject request = new SoapObject(Soap_NameSpace, Soap_MethodName);
	//Tao arraylist lay log tu db
	final Object obj = new Object();
  	ArrayList<String> arrFilmCatName = new ArrayList<String>();
  	ArrayList<Integer> arrFilmCatID = new ArrayList<Integer>();
	VideoAdapter adpListVideo;
	public static int BoLe=-1;
	public String parent="<=0";
	//Dialog the hien tien trinh
	ProgressDialog progress; 
	Context ctx;
	public getFilmCat(Context ctx) {
        this.ctx = ctx;
        progress = new ProgressDialog(ctx);
    }
	
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		progress.dismiss();
		//Cap nhat den class video data
		FilmCatData.lstFilmCatName=arrFilmCatName;
		FilmCatData.lstFilmCatID = arrFilmCatID;
		Log.d("So phim categories FilmCatData :::::::", ""+FilmCatData.lstFilmCatID.size());
		//Load danh sach cho actMovie activity
		actFilmMain3.UpdateFilmCat();
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		//Chay progress dialog
		progress.setMessage("Loading film categories...");
		progress.setIndeterminate(true);
		progress.setCancelable(false);
		progress.show();
	}

	@Override
	protected void onProgressUpdate(Void... values) {
		// TODO Auto-generated method stub
		super.onProgressUpdate(values);
	}

	@Override
	protected String doInBackground(String... params) {
		// TODO Auto-generated method stub
		if (BoLe==-1) {		//Nếu là tất cả
			parent="<=0";
		}
		if (BoLe==0) {		//Phim bộ
			parent="=0";
		}
		if (BoLe==1) {		//Phim lẻ
			parent="=-1";
		}
		
		final SoapSerializationEnvelope Soap_Evelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
        Soap_Evelope.dotNet=true;
        final HttpTransportSE androidHttpTransport = new HttpTransportSE(Soap_URL);
        request.addProperty("ParentID",parent);
		Soap_Evelope.setOutputSoapObject(request);
		try{
			androidHttpTransport.call(Soap_Action, Soap_Evelope);
			SoapObject respond = (SoapObject) Soap_Evelope.bodyIn;
			respond = (SoapObject) Soap_Evelope.getResponse();
			//loc cac thong tin thua
			respond = (SoapObject) respond.getProperty(0);
            //Lay so dong du lieu da nhan ve
            int x = respond.getPropertyCount();
            Log.d("So phim categories :::::::", ""+x);
            if(x==0){
            }
            else{
            	//Neu du lieu nhan ve >0 dong
            	//==================Lay thong tin can thiet====================
	            for (int y=0;y<x;y++){
	            	SoapObject respond2 = (SoapObject) respond.getProperty(y);
	            	String name="";
	            	int id=-1;
	            	name=respond2.getProperty("CategoryName").toString();
	            	arrFilmCatName.add("   + "+name);
	            	Log.d("film cat:::::", name);
	            	id=Integer.parseInt(respond2.getProperty("CategoryID").toString());
	            	arrFilmCatID.add(id);
	            }
	            //=============================================================
            }
			
		} catch (IOException e) {
			
		}
		 catch (XmlPullParserException e) {
			
		} catch (ClassCastException e) {
			
		}
		return null;
}
}
